package com.matrix.index.ui.home.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import com.blankj.utilcode.util.LogUtils
import com.matrix.basecore.net.BaseData
import com.matrix.index.net.IndexResult
import com.matrix.index.respository.WallpaperResp
import com.matrix.index.ui.home.bean.WallpaperBean
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow

class WallpaperHotVM : ViewModel() {

    /**
     * limit: Int,
     * skip: Int,
     * adult: Boolean,
     * first: Int,
     * order: String
     * limit=12&skip=12&adult=false&first=0&order=hot
     */
    fun getHotNewWallpaper(
        skip: Int,
        order: String
    ): LiveData<IndexResult<out List<BaseData>>> {
        return flow {
            val resultData = WallpaperResp.getHotNewWallpaper(12, skip, false, 0, order)
            if (resultData.code == 0) {
                var indexResult = IndexResult(IndexResult.SUCCESS, resultData.res!!.vertical)
                emit(indexResult)
            } else {
                //处理错误
                LogUtils.i(resultData.msg)
                var indexResult = IndexResult<List<BaseData>>(IndexResult.ERROR, resultData.msg)
                emit(indexResult)
            }
        }.catch { ex ->
            var indexResult = IndexResult<List<BaseData>>(IndexResult.ERROR, ex.toString())
            emit(indexResult)
        }.asLiveData()
    }
}